#ifndef XLCOMM_NET_RPCCHANNELSERVER_H_
#define XLCOMM_NET_RPCCHANNELSERVER_H_

#include <google/protobuf/service.h>

#include "xlcomm/define.h"
#include "xlcomm/net/protorpc/rpc.pb.h"
#include "xlcomm/net/protorpc/rpc_message_codec.h"
#include "xlcomm/net/tcp_server.h"
#include "xlcomm/noncopyable.h"

namespace xlcomm {
namespace net {

class XLCOMM_API RpcChannelServer : public google::protobuf::RpcChannel,
                                    noncopyable {
 public:
  RpcChannelServer(net::EventLoop* loop, const net::InetAddress& listen_addr);

  void Start() { server_.Start(); }

  void RegisterService(google::protobuf::Service* service);

  void CallMethod(const google::protobuf::MethodDescriptor* method,
                  google::protobuf::RpcController* controller,
                  const google::protobuf::Message* request,
                  google::protobuf::Message* response,
                  google::protobuf::Closure* done) override {
    LOG_DEBUG << "RpcChannelServer::CallMethod==== in";
  }

 private:
  using ServiceMap = std::map<std::string, google::protobuf::Service*>;

  void OnRpcMessage(const net::TcpConnectionPtr& conn,
                    const RpcMessage& rpcmsg);

  net::EventLoop* loop_;
  net::TcpServer server_;
  ServiceMap services_;
  RpcMessageCodec codec_;
};

}  // namespace net
}  // namespace xlcomm

#endif  // XLCOMM_NET_RPCCHANNELSERVER_H_